const express = require('express')


//创建一个路由对象 
const router = express.Router()

//导出路由对象
module.exports = router;


router.use(
    express.json(),
    express.urlencoded({extended:true}), 
)

const db = require('../db');
const {formatData} = require('../utils')


//注册
// /api/reg
router.post('/reg',(req,res)=>{
    console.log(6666);
    const { username, password,email } = req.body;
    let sql = `insert into user(username,password,email) values('${username}','${password}','${email}')`
    db.query(sql,(result)=>{
        if(result.insertId){
            res.send(formatData.success())
        }else{
            res.send(formatData({code:400}))
        }
    })

})

//@登录    /api/login
router.post('/login',(req,res)=>{
    const {username,password} = req.body;
    let sql = `select * from user where username='${username}' and password='${password}'`
    db.query(sql,(result)=>{
        if(result.length>0){
            res.send(formatData.success(result[0]))
        }else{
            res.send(formatData.fail())
        }
    })
})

//用户访问  http://localhost:3000/view  时，如用户使用的是PC浏览器则响应  趣学旅程PC端  字样，如用户使用的是移动端浏览器则响应  趣学旅程移动端  字样

router.get('/view',(req,res)=>{
    const UserAgent = req.get('User-Agent');
    console.log(UserAgent);
    console.log(6666);
    if(UserAgent.indexOf('Windows')>=0){
        res.send(' 趣学旅程PC端 ')
    }else{
        res.send('趣学旅程移动端 ')
    }
})    
        
// //引入路由对象
const userRouter = require('./user')
// const goodsRouter = require('./goods')
// const cartRouter = require('./cart')


// //2、接口服务器 模块化路由  创建总路由对象
// //        /api/user
router.use('/user',userRouter)
// //        /api/goods
// router.use('/goods',goodsRouter)
// //        /api/cart
// router.use('/cart',cartRouter)
